package com.eschool.beans;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.model.DualListModel;
import org.primefaces.model.UploadedFile;

import com.eschool.beans.converter.CourseConverter;
import com.eschool.beans.converter.UserConverter;
import com.eschool.dao.CourseDao;
import com.eschool.model.Course;
import com.eschool.model.Teacher;

import com.eschool.service.TeacherService;
import com.eschool.service.impl.TeacherServiceImpl;

@ManagedBean(name="assignCourseBean")
@ViewScoped
public class AssignUnassignCourseBean {

	private DualListModel <Course> courses;
	private Teacher selectedTeacher;
	private List<Teacher> allTeachers;
	private UserConverter teacherConverter;
	
	
	
	TeacherService teacherService = new TeacherServiceImpl();
	public Teacher getSelectedTeacher() {
		return selectedTeacher;
	}

	public void setSelectedTeacher(Teacher selectedTeacher) {
		this.selectedTeacher = selectedTeacher;
	}

	public List<Teacher> getAllTeachers() {
		return allTeachers;
	}

	public void setAllTeachers(List<Teacher> allTeachers) {
		this.allTeachers = allTeachers;
	}

	public UserConverter getTeacherConverter() {
		return teacherConverter;
	}

	public void setTeacherConverter(UserConverter teacherConverter) {
		this.teacherConverter = teacherConverter;
	}
	
	

	public List<Teacher> completeTeacher(String query) {
		List<Teacher> userSuggestions = new ArrayList<Teacher>();  
        
        for(Teacher t : allTeachers) {  
            if(t.getName().toLowerCase().startsWith(query.toLowerCase().trim()))  
            	userSuggestions.add(t);  
        }  
          
        return userSuggestions; 
	}
	
		
	

	public DualListModel<Course> getCourses() {
		return courses;
	}

	public void setCourses(DualListModel<Course> courses) {
		this.courses = courses;
	}

	public AssignUnassignCourseBean() {
		courses = new DualListModel<Course>(CourseConverter.courseDB,Collections.EMPTY_LIST);
	}

	
	public String perform()
	{
		
		selectedTeacher.setCourses(new HashSet<Course>(courses.getTarget()));
		return "Course Assigned";
}
}
